﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Net.Mail;
using System.Text;
using TSqlServer;

public partial class system_sendmail : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string userid = Request["userid"].ToString();
            string mailtitle = Request["mailtitle"].ToString();
            string sendman = Request["sendman"].ToString();
            string mailaddr = Request["mailaddr"].ToString();
            string mailcontent = Request["mailcontent"].ToString();
            int poster = Convert.ToInt32(Request["poster"].ToString());

            mysendmail(userid,sendman, mailaddr, mailtitle, mailcontent,poster);
        }
    }

    private bool mysendmail(string uid,string sendman,string email,string mailtitle,string mailcontent,int posterid)
    {
        try
        {
            MailAddress fromemail = new MailAddress("admin@cnap.com.cn",sendman);

            /*
            if (posterid % 2 == 0)
            {
                fromemail.Address = "online@cnap.com.cn";
            }
            else
            {
                fromemail.Address = "admin@cnap.com.cn";
            }
            fromemail.DisplayName = sendman;
            */

            MailAddress toemail = new MailAddress(email);

            MailMessage oMail = new MailMessage(fromemail, toemail);
            oMail.Subject = mailtitle;
            oMail.SubjectEncoding = Encoding.GetEncoding("GB2312");
            //oMail.SubjectEncoding = System.Text.Encoding.UTF8;
            oMail.Priority = MailPriority.Normal;
            oMail.IsBodyHtml = true;
            oMail.Body = mailcontent;
            oMail.BodyEncoding = Encoding.GetEncoding("GB2312");
            //oMail.BodyEncoding = System.Text.Encoding.UTF8;

            SmtpClient oSmtp = new SmtpClient("smtp.cnap.com.cn");
            //SmtpClient oSmtp = new SmtpClient("211.144.35.45");
            //oSmtp.Credentials = new System.Net.NetworkCredential("admin@cnap.com.cn", "mala990707");

            if (posterid % 2 == 0)
            {
                oSmtp.Credentials = new System.Net.NetworkCredential("online@cnap.com.cn", "mala990707");
            }
            else
            {
                oSmtp.Credentials = new System.Net.NetworkCredential("admin@cnap.com.cn", "mala990707");
            }

            //oSmtp.DeliveryMethod = SmtpDeliveryMethod.Network;
            //oSmtp.UseDefaultCredentials = true;
            oSmtp.Send(oMail);

            string sSql = "insert into t_sendmail_log2 (mailtitle,mailaddr,uid,iflag) values('" + mailtitle + "','" + email + "','" + uid + "',0)";
            TSqlDB.ExecuteNonQuery(sSql);

            return true;
        }
        catch (Exception e)
        {
            string sSql = "insert into t_sendmail_log2 (mailtitle,mailaddr,uid,iflag) values('" + mailtitle + "','" + email + "','" + uid + "',1)";
            TSqlDB.ExecuteNonQuery(sSql);

            return false;
        }
    }
}
